package com.sgl.threads;

import com.sgl.entity.AcceptPayOrder;
import com.sgl.entity.MybatisParamVo;
import com.sgl.entity.OrderHistory;
import com.sgl.service.AcceptPayOrderService;
import com.sgl.service.OrderService;
import com.sgl.util.HttpUtil;
import com.sgl.util.PageBean;
import com.sgl.util.SpringBeanUtil;
import com.sgl.util.StringSingUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DoNotify implements Runnable {

    private  AcceptPayOrderService acceptPayOrderService;

    private OrderService orderService;

    private static final Log log = LogFactory.getLog(DoNotify.class);

    private Map<String, String> paramMap;

    private String notifyUrl;

    private Long orderId;

    private String key;
    //需要什么参数就自己定义
    public DoNotify(Map<String, String> paramMap,String key,String notifyUrl,Long acceptOrderId) {
        this.paramMap = paramMap;
        this.key=key;
        this.notifyUrl=notifyUrl;
        this.orderId=acceptOrderId;
    }

    public DoNotify(){


    }


    @Override
    public void run() {
        this.acceptPayOrderService=SpringBeanUtil.getBean(AcceptPayOrderService.class);
        this.orderService=SpringBeanUtil.getBean(OrderService.class);
        String sign= StringSingUtils.getSign2(paramMap, key);
        paramMap.put("pay_sign", sign);
        try{
            log.info("=============开始异步通知crm支付结果=============");
                String result = HttpUtil.doPost(notifyUrl, paramMap);
           /* String s="OK";*/
            if (result.indexOf("OK")>-1|| result.indexOf("200")>-1){
                log.info("=============CRM系统返回的信息： "+result+"   =============" );
                log.info("将接受到的预处理订单修改为已处理");
                //todo  解决这里的空指针的问题
                acceptPayOrderService.changeStatiusByid(orderId);
                //todo 将处理过的订单改为 已处理
                log.info("=============orderHistory订单改为回调成功=============");
                PageBean<OrderHistory> pageBean = new PageBean<>();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("merOrderId",paramMap.get("pay_orderid"));
                pageBean.setParam(hashMap);
                pageBean.setPageSize(100);
                //查找是这个订单的ordershitory 以防万一用的list
                List<OrderHistory> orderHistorys = orderService.findOrderByMerOrderId(pageBean);
                MybatisParamVo<OrderHistory> paramVo = new MybatisParamVo<>();
                paramVo.setParams(orderHistorys);
                paramVo.setStatus(1);
                //将这类订单状态改为 正在重复回调
                Integer integer = orderService.changeStatusByParam(paramVo);
                log.info("==============更改了： "+integer+" 条订单 为处理完成订单==============");
            }else {
                log.info("=============orderHistory订单改为准备重复回调状态=============");
                AcceptPayOrder order = acceptPayOrderService.getPayOrderByOrderId(paramMap.get("pay_orderid"));
                if (null==order){
                    log.error("ERROR=============未找到crm建单信息=============ERROR");
                    return;
                }
                /*order.setStatus("3");
                acceptPayOrderService.changeStatusByIdOrder(order);*/
                PageBean<OrderHistory> pageBean = new PageBean<>();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("merOrderId",order.getPayOrderid());
                pageBean.setParam(hashMap);
                //查找是这个订单的ordershitory 以防万一用的list
                List<OrderHistory> orderHistory = orderService.findOrderByMerOrderId(pageBean);
                //一般情况下这里只会有一个 但是可能会出现过个
                MybatisParamVo<OrderHistory> paramVo = new MybatisParamVo<>();
                paramVo.setParams(orderHistory);
                paramVo.setStatus(2);
                //将状态改为 正在重复回调
                orderService.changeStatusByParam(paramVo);

            }

        }catch (Exception e){
            e.printStackTrace();
            log.error("ERROR=============异步通知crm出现错误=============ERROR");
            log.info("=============orderHistory订单改为准备重复回调状态=============");
            AcceptPayOrder order = acceptPayOrderService.getPayOrderByOrderId(paramMap.get("pay_orderid"));
            if (null==order){
                log.error("ERROR=============未找到crm建单信息=============ERROR");
                return;
            }
                /*order.setStatus("3");
                acceptPayOrderService.changeStatusByIdOrder(order);*/
            PageBean<OrderHistory> pageBean = new PageBean<>();
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("merOrderId",order.getPayOrderid());
            pageBean.setParam(hashMap);
            //查找是这个订单的ordershitory 以防万一用的list
            List<OrderHistory> orderHistory = orderService.findOrderByMerOrderId(pageBean);
            //一般情况下这里只会有一个 但是可能会出现过个
            MybatisParamVo<OrderHistory> paramVo = new MybatisParamVo<>();
            paramVo.setParams(orderHistory);
            paramVo.setStatus(2);
            //将状态改为 正在重复回调
            orderService.changeStatusByParam(paramVo);

            e.printStackTrace();

        }

    }
}
